package pers.fjl.test.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import pers.fjl.common.entity.QueryPageBean;
import pers.fjl.common.po.Permission;
import pers.fjl.common.po.Role;
import pers.fjl.common.po.Warehouse;
import pers.fjl.common.vo.PermissionVo;
import pers.fjl.test.dao.PermissionDao;
import pers.fjl.test.dao.RoleDao;
import pers.fjl.test.dao.WarehouseDao;
import pers.fjl.test.service.RoleService;
import pers.fjl.test.service.WarehouseService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class RoleServiceImpl extends ServiceImpl<RoleDao, Role> implements RoleService {
    @Resource
    private RoleDao roleDao;
    @Resource
    private PermissionDao permissionDao;

    @Cacheable(value = {"RoleMap"})
    public Page<Role> findPage(QueryPageBean queryPageBean) {
        //设置分页条件
        Page<Role> page = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //设置查询条件
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.like(queryPageBean.getQueryString() != null, "role_name", queryPageBean.getQueryString());
        return roleDao.selectPage(page, wrapper);
    }

    @Override
    public List<PermissionVo> getMenu(Integer rid) {
        List<PermissionVo> permissionVos = new ArrayList<>();
        List<PermissionVo> childPermissionVos = new ArrayList<>();

        PermissionVo permissionVo = new PermissionVo();
        PermissionVo permissionVo2 = new PermissionVo();
        List<Permission> permissions = permissionDao.getOwn(rid);

        for (Permission permission : permissions) {
            BeanUtils.copyProperties(permission, permissionVo);
            if (permission.getPath() == null) { //代表是一级目录
                //
                for (Permission permission2 : permissions) { //找出一级目录下的二级目录，并添加到一级目录的children属性
                    if (permission2.getPath() != null && permission2.getPid().equals(permission.getId())) {  //找出属于一级目录对应的二级目录
                        BeanUtils.copyProperties(permission2, permissionVo2);
//                        System.out.println("一级目录是:" + permission.getAuthName() + "====>" + permission);
//                        System.out.println("二级目录是:" + permission2.getAuthName() + "====>" + permission2);
                        childPermissionVos.add(permissionVo2);
                        permissionVo2 = new PermissionVo();
                    }
                }
                permissionVo.setChildren(childPermissionVos);
                permissionVos.add(permissionVo);
                permissionVo = new PermissionVo();
                childPermissionVos = new ArrayList<>();
            }
        }
//        System.out.println("列表list====》" + permissionVos);
        return permissionVos;
    }
}
